package com.github.catvod.spider.merge;

import com.github.catvod.crawler.SpiderDebug;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/* loaded from: classes.dex */
public class fP {
    private static OkHttpClient V0;
    private static OkHttpClient j;
    private static final Object h = new Object();
    private static final MediaType q = MediaType.parse("application/x-protobuf");

    static class Tw extends L<String> {
        final /* synthetic */ Map V0;

        Tw(Map map) {
            this.V0 = map;
        }

        @Override // com.github.catvod.spider.merge.L
        public void onFailure(Call call, Exception exc) {
            V0("");
            SpiderDebug.log(exc);
        }

        @Override // com.github.catvod.spider.merge.L
        public String onResponse(String str) {
            return str;
        }

        @Override // com.github.catvod.spider.merge.L
        public String onParseResponse(Call call, Response response) {
            try {
                Map map = this.V0;
                if (map != null) {
                    map.clear();
                    this.V0.putAll(response.headers().toMultimap());
                }
                return new String(response.body().bytes(), "GB2312");
            } catch (IOException unused) {
                return "";
            }
        }
    }

    static class yh extends L<String> {
        final /* synthetic */ Map V0;

        yh(Map map) {
            this.V0 = map;
        }

        @Override // com.github.catvod.spider.merge.L
        public void onFailure(Call call, Exception exc) {
            V0("");
            SpiderDebug.log(exc);
        }

        @Override // com.github.catvod.spider.merge.L
        public String onResponse(String str) {
            return str;
        }

        @Override // com.github.catvod.spider.merge.L
        public String onParseResponse(Call call, Response response) {
            try {
                Map map = this.V0;
                if (map != null) {
                    map.clear();
                    this.V0.putAll(response.headers().toMultimap());
                }
                return response.body().string();
            } catch (IOException unused) {
                return "";
            }
        }
    }

    public static void BQ(OkHttpClient okHttpClient, String str, Map<String, String> map, Map<String, String> map2, L l) {
        new g("POST", str, map, map2, l).h(okHttpClient);
    }

    public static String F8(String str, Map<String, String> map, Map<String, List<String>> map2) {
        return t(j(), str, null, null, map, map2);
    }

    public static String I(Map<String, List<String>> map) {
        if (map == null) {
            return null;
        }
        if (map.containsKey("location")) {
            return map.get("location").get(0);
        }
        if (map.containsKey("Location")) {
            return map.get("Location").get(0);
        }
        return null;
    }

    public static String Qy(String str, String str2, Map<String, String> map) {
        return t(j(), str, str2, null, map, null);
    }

    public static void V0(OkHttpClient okHttpClient, Object obj) {
        if (okHttpClient == null || obj == null) {
            return;
        }
        for (Call call : okHttpClient.dispatcher().queuedCalls()) {
            if (obj.equals(call.request().tag())) {
                call.cancel();
            }
        }
        for (Call call2 : okHttpClient.dispatcher().runningCalls()) {
            if (obj.equals(call2.request().tag())) {
                call2.cancel();
            }
        }
    }

    public static void W(OkHttpClient okHttpClient, String str, String str2, Map<String, String> map, L l) {
        new g("POST", str, str2, map, l).h(okHttpClient);
    }

    public static void h(Object obj) {
        V0(j(), obj);
    }

    public static String i4(String str, Map<String, String> map, Map<String, List<String>> map2) {
        return t(oC(), str, null, null, map, map2);
    }

    public static String iU(OkHttpClient okHttpClient, String str, String str2, Map<String, String> map, Map<String, String> map2, Map<String, List<String>> map3) {
        Tw tw = new Tw(map3);
        g gVar = new g("GET", str, map, map2, tw);
        gVar.oC(str2);
        gVar.h(okHttpClient);
        return tw.getResult();
    }

    public static OkHttpClient j() {
        OkHttpClient okHttpClient;
        synchronized (h) {
            if (V0 == null) {
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                TimeUnit timeUnit = TimeUnit.SECONDS;
                OkHttpClient.Builder retryOnConnectionFailure = builder.readTimeout(15L, timeUnit).writeTimeout(15L, timeUnit).connectTimeout(15L, timeUnit).retryOnConnectionFailure(true);
                X509TrustManager x509TrustManager = k6.j;
                V0 = retryOnConnectionFailure.sslSocketFactory(new k6(x509TrustManager), x509TrustManager).build();
            }
            okHttpClient = V0;
        }
        return okHttpClient;
    }

    public static String n(String str, Map<String, String> map) {
        return iU(j(), str, null, null, map, null);
    }

    public static OkHttpClient oC() {
        OkHttpClient okHttpClient;
        synchronized (h) {
            if (j == null) {
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                TimeUnit timeUnit = TimeUnit.SECONDS;
                OkHttpClient.Builder retryOnConnectionFailure = builder.readTimeout(15L, timeUnit).writeTimeout(15L, timeUnit).connectTimeout(15L, timeUnit).followRedirects(false).followSslRedirects(false).retryOnConnectionFailure(true);
                X509TrustManager x509TrustManager = k6.j;
                j = retryOnConnectionFailure.sslSocketFactory(new k6(x509TrustManager), x509TrustManager).build();
            }
            okHttpClient = j;
        }
        return okHttpClient;
    }

    public static String oq(String str, Map<String, String> map) {
        return t(j(), str, null, null, map, null);
    }

    public static void q(OkHttpClient okHttpClient, String str, Map<String, String> map, Map<String, String> map2, L l) {
        new g("GET", str, map, map2, l).h(okHttpClient);
    }

    public static String t(OkHttpClient okHttpClient, String str, String str2, Map<String, String> map, Map<String, String> map2, Map<String, List<String>> map3) {
        yh yhVar = new yh(map3);
        g gVar = new g("GET", str, map, map2, yhVar);
        gVar.oC(str2);
        gVar.h(okHttpClient);
        return yhVar.getResult();
    }

    public static void u(OkHttpClient okHttpClient, String str, Map<String, String> map, Map<String, String> map2, Map<String, List<String>> map3) {
        new k("POST", str, map, map2, map3).h(okHttpClient);
    }
}
